<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * An APC driver implementation of Pfw_Cache.
 *
 * PHP version 5
 *
 * Copyright 2008 The Picnic PHP Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @category      Framework
 * @package       Pfw
 * @author        Sean Sitter <sean@picnicphp.com>
 * @copyright     2008 The Picnic PHP Framework
 * @license       http://www.apache.org/licenses/LICENSE-2.0
 * @link          http://www.picnicphp.com
 * @since         0.10
 */

Pfw_Loader::loadClass('Pfw_Cache');

class Pfw_Cache_Apc implements Pfw_Cache
{
	// default option constants
    const DEFAULT_TTL_S = 300;

    protected static $apc_exists = null;
    private $options;

    /**
     * @param array $options containing keys
     * 'default_ttl_s' for default ttl in seconds 
     */
    public function __construct($options = array()) {
        $this->options = $options;

        if (!isset($this->options['default_ttl_s'])) {
            $this->options['default_ttl_s'] = self::DEFAULT_TTL_S;
        }
    }

    
    /**
     * @see Pfw_Cache#get()
     */
    public function get($key)
    {
        if (!$this->isActive()) {
            // apc isn't running
            return false;
        }

        if (false !== ($value = apc_fetch($key))) {
            // value exists, return it
            return unserialize($value);
        }

        // value didn't exist
        return false;
    }
    

    /**
     * @see Pfw_Cache#set()
     */
    public function set($key, $value, $ttl_s = null)
    {
        if (!$this->isActive()) {
            return false;
        }
        if (null === $ttl_s) {
            $ttl_s = $this->options['default_ttl_s'];
        }

        return apc_store($key, serialize($value), $ttl_s);
    }

    
    /**
     * @see Pfw_Cache#add()
     */
    public function add($key, $value, $ttl_s = null)
    {
        if (!$this->isActive()) {
            return false;
        }
        if (null === $ttl_s) {
            $ttl_s = $this->options['default_ttl_s'];
        }

        return apc_add($key, serialize($value), $ttl_s = null);
    }

    
    /**
     * @see Pfw_Cache#delete()
     */
    public function delete($key)
    {
        if (!self::isActive()) {
            return false;
        }

        return apc_delete($key);
    }


    /**
     * @see Pfw_Cache#isActive()
     */
    public function isActive()
    {
        if (null === self::$apc_exists) {
            self::$apc_exists = (function_exists('apc_fetch')) ? true : false;
        }
        return self::$apc_exists;
    }
}
